'use strict'

const { ApiController } = require('jax')

class Controller extends ApiController {
  get Model() {
    return this.ctx.model.StoreUser
  }

  // 匹配查询
  get searchKey() {
    return ['isOpen', 'app']
  }

  // 模糊查询
  get likeKey() {
    return ['categoryName']
  }

  // 查询前
  async beforeIndex() {
    const { ctx } = this
    const { query } = ctx
    const { helper } = ctx
    const { SYSTEM_TYPE_STORE } = this.config.constant

    if (this.admin.systemType != SYSTEM_TYPE_STORE) throw '非法操作'

    const filter = {
      storeId: this.admin.sourceId,
    }

    // 关键字模糊查询
    if (query.keyword && !query.keyword == '') {
      const reg = new RegExp(helper.toMongoRegex(query.keyword, 'i'))
      filter.$or = [{ categoryName: reg }]
    }

    return {
      filter,
    }
  }

  // 导出
  async exportConfig() {
    return [
      { name: '姓名', key: 'name' },
      { name: '手机号', key: 'mobile' },
      { name: '预约日期', key: 'activityDate' },
    ]
  }
}

module.exports = Controller
